<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sign_model extends CI_Model {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */



	public function queryListData($pId) {

		//每页数据条目
		$pageItemCount=20;

		$show_page=7;

		$pageOffset=($show_page-1)/2;

		$offset=($pId-1)*$pageItemCount;


		$OUTPUT=array();

		$this->load->database();

		//使用CI内置数据库查询方法，简化查询语句
		$this->db->select('slide_id,title,thumbnail,summary,view_count,publish_date');

		$OUTPUT['data']=$this->db->order_by('publish_date','DESC')->get_where('slide_item_tb',array('audit_status'=>1), $pageItemCount,$offset)->result_array();



		if(empty($OUTPUT['data'])){
			return NULL;
			exit;
		}




		$OUTPUT['recordCount']=$this->db->count_all('slide_item_tb');

		//总页数
		$OUTPUT['total_pages']=intval(ceil($OUTPUT['recordCount']/$pageItemCount));
		$OUTPUT['page']=intval($pId);
		$OUTPUT['show_page']=$show_page;
		$OUTPUT['pageOffset']=$pageOffset;
		$OUTPUT['list_base_url']='/book/list?';

		return $OUTPUT;

	}

	
	public function queryListBy($sortByField='view_count',$pageItemCount=60)
	{


		$this->load->database();

		//使用CI内置数据库查询方法，简化查询语句
		$this->db->select('slide_id,title,thumbnail,summary,view_count,publish_date');
		
		$data=$this->db->order_by($sortByField,'DESC')->get_where('slide_item_tb',array('audit_status'=>1),$pageItemCount,0)->result_array();


		if(empty($data)){
			return NULL;
			exit;
		}

		return $data;

	}




	public function queryItemData($pId){
		$OUTPUT=array();

		$this->load->database();

		//使用CI内置数据库查询方法，简化查询语句
		$this->db->select('slide_id,title,thumbnail,summary,content,view_count,create_date,publish_date');


		$OUTPUT=$this->db->get_where('slide_item_tb', array('slide_id' => $pId,'audit_status'=>1))->row_array();

		if(empty($OUTPUT)){
			return NULL;
			exit;
		}
		
		

		if($OUTPUT['publish_date']){
			$OUTPUT['publish_date']=date('Y-m-d',strtotime($OUTPUT['publish_date']));
		}
		

		$OUTPUT['pId']=$pId;

		return $OUTPUT;

	}



	public function getRelevant($bookId)
	{


 		//每页数据条目
 		$pageItemCount=5;

		$this->load->database();

         $offset=($bookId-1)*$pageItemCount;

         //使用CI内置数据库查询方法，简化查询语句
         $this->db->select('book_id,title,small_cover');


         //筛选资料完整的图书
         $this->db->where('status',1);
         $this->db->where('title is not null');
         $this->db->where('small_cover is not null');
         $this->db->order_by('book_id','RANDOM');


         $OUTPUT['data']=$this->db->get('slide_item_tb',$pageItemCount)->result_array();

         $OUTPUT['code']=1;

		return $OUTPUT;

	}


	
	


}
